﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class ComposeMail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.ComposeMail_PageTitle;
            HistoryLabel.Visible = false;
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            
            OnlineUser ou = (OnlineUser)Session["User"];
            
            if (ou.TeamId == "Null")
                Response.Redirect("~/GeneralMessage.aspx?MessageString=M74ggdizz5wfeh");

            
            if (!IsPostBack)
            {
                string ToId = null;
                if (Request.QueryString["ToId"] != null)
                {
                    ToId = Request.QueryString["ToId"].ToString();
                    ToIdHiddenField.Value = ToId;
                    UserPicImage.ImageUrl = "~/Images/Icons/" + GND.ReadNivdalStringData("@UserId", "Nivdal_GetUserIcon", ToId);
                    UserPicImage.ToolTip = GND.ReadNivdalStringData("@UserId", "@Language", "Nivdal_GetAccessLevelStringProc", ToId, ou.Language);
                    UserPageLinkButton.Text = GND.ReadNivdalStringData("@UserId", "Nivdal_GetUserNameById", ToId);
                    UserPageLinkButton.NavigateUrl = "UserPage.aspx?Id=" + ToId;
                }
                else
                {
                    Response.Redirect("~/Default.aspx");
                }

                string Subject = null;
                if (Request.QueryString["Subject"] != null)
                {
                    Subject = Request.QueryString["Subject"].ToString();
                    SubjectTextBox.Text = Subject;
                }

                string IsReplyTo = null;
                if (Request.QueryString["IsReplyTo"] != null)
                {
                    IsReplyTo = Request.QueryString["IsReplyTo"].ToString();
                    IsReplyToHiddenField.Value = IsReplyTo;
                    if (IsReplyTo != "0")
                    {
                        if (GND.ReadNivdalBoolData("UserId", "MailId", "Nivdal_MailCanUserSeeMail", ou.UserId, IsReplyTo))
                        {
                            HistoryLabel.Visible = true;
                            string PreText = null;
                            PreText = (GND.ReadNivdalStringData("@MailId", "Language", "Nivdal_MailGetMailHistory", IsReplyTo, ou.Language)).Replace("\r\n", "<br /> ");
                            if (PreText == null)
                                Response.Redirect("~/Default.aspx");
                            //HistoryLabel.Text = GlobalFunctions.EncodeHtmlFromText(PreText);
                            HistoryLabel.Text = PreText;
                        }
                        else
                        {
                            Response.Redirect("GeneralMessage.aspx?MessageString=haoihxrjmm");
                        }
                    }
                }
            }

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            string Subject = SubjectTextBox.Text.Trim();
            string Body = NewTextArea.Text.Trim();
            OnlineUser ou = (OnlineUser)Session["User"];
            string ToId = ToIdHiddenField.Value;
            string IsReplyTo = IsReplyToHiddenField.Value;

            string conn = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            System.Data.SqlClient.SqlConnection
            strConnection = new System.Data.SqlClient.SqlConnection(conn);
            string StrSQL = "Nivdal_MailSendMail";
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(StrSQL, strConnection);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Sender", ou.UserId));
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Receiver", ToId));
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Subject", Subject));
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Body", Body));
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@IsReplyTo", IsReplyTo));
            strConnection.Open();
            try
            {
                cmd.ExecuteReader();
                Response.Redirect("MailSentItems.aspx");
            }
            catch (Exception SQLDBException)
            {
                Label1.Text = Convert.ToString(SQLDBException);
            }

            finally
            {
                strConnection.Close();
            }
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            //GlobalFunctions GF = new GlobalFunctions();
            this.PreviewLabel.Text = "<br /><br /> " + GlobalFunctions.EncodeHtmlFromText(this.NewTextArea.Text.Trim());
        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }
    }
}